package com.blockmeta.bbs.businesslibrary.voice;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;


import com.blockmeta.bbs.baselibrary.utils.AnimatorUtil;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by author on 2018/5/3.
 * 百度语音业务接口  全部抽成一个业务类
 */

public class BaiduVoiceBusiness {

    public static List<String> setVoiceText(String title, String content) {
       List<String> speaks = new ArrayList<>();
        speaks.add(title);
        Document parse = Jsoup.parse(content);
        Element body = parse.body();
        Elements children = body.children();
        if (children.size() > 20) {
            String text = children.text();
            int index = 0;
            int length = text.length();
            while (index < length && index + 500 < length) {
                speaks.add(text.substring(index, index + 500));
                index += 500;
            }
            speaks.add(text.substring(index, length));
        } else {
            for (int i = 0; i < children.size(); i++) {
                if (!TextUtils.isEmpty(children.get(i).text())) {
                    if (children.get(i).text().length() > 510) {
                        int index = 0;
                        int length = children.get(i).text().length();
                        String more512 = children.get(i).text();
                        while (index < length && index + 500 < length) {
                            speaks.add(more512.substring(index, index + 500));
                            index += 500;
                        }
                        speaks.add(more512.substring(index, length));
                    } else {
                        speaks.add(children.get(i).text());

                    }
                }
            }
        }
        return speaks;

    }

    public static boolean setBaiduVoiceData(Context mContext, boolean isClickPlayer, BaiduVoice mBaiduVoice,
                                         List<String> speaks, View fab, TextView mPlayer) {
        if (!isClickPlayer) {
            mBaiduVoice.speak(speaks);
            isClickPlayer = true;
//            setPlayerUI(mContext, mPlayer, isClickPlayer);
            fab.setVisibility(View.VISIBLE);
            AnimatorUtil.rotateStart(fab);
        } else {
            mBaiduVoice.stop();
            isClickPlayer = false;
//            setPlayerUI(mContext, mPlayer, isClickPlayer);
            fab.setVisibility(View.GONE);
            AnimatorUtil.rotatePause(fab);
        }
        return isClickPlayer;
    }

//    public static void setPlayerUI(Context mContext, TextView tv, boolean b) {
//        tv.setTextColor(b == true ? mContext.getResources().getColor(R.color.base_color_red) : mContext.getResources().getColor(R.color.text_black));
//        tv.setCompoundDrawablesWithIntrinsicBounds(
//                b ? R.drawable.voice_red : R.drawable.voice_gray, 0, 0, 0);
//    }


    /**
     * android 6.0 以上需要动态申请权限
     */
    public static void initPermission(BaseActivity activity) {
        String permissions[] = {
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_SETTINGS,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE
        };
        ArrayList<String> toApplyList = new ArrayList<String>();
        for (String perm : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activity, perm)) {
                toApplyList.add(perm);
                //进入到这里代表没有权限.
            }
        }
        String tmpList[] = new String[toApplyList.size()];
        if (!toApplyList.isEmpty()) {
            ActivityCompat.requestPermissions(activity, toApplyList.toArray(tmpList), 123);
        }
    }

}
